<?php

namespace app\company\model;


use Exception;
use think\Model;

class WarehouseStorage extends Base
{

    /**
     * @param WarehouseStorage $model
     * @return void
     * @throws Exception
     */
    public static function onBeforeUpdate(WarehouseStorage $model): void
    {
        //商品移位操作  注意: 只能移至同库区中库位(产品需求)
        if ($storageId = request()->param('new_warehouse_storage_id', 0)) {
            if (!$info = self::find($storageId)) exception('移至的库位不存在!');
            if ($model->companyOrderStorages()->sum('quantity') == 0) exception('该库位没有商品,不能移位!');
            $model->companyOrderStorages->update(['warehouse_storage_id' => $storageId, 'warehouse_storage_name' => $info['name']]);
        }
    }

    /**
     * @param WarehouseStorage $model
     * @return void
     * @throws Exception
     */
    public static function onBeforeDelete(WarehouseStorage $model): void
    {
        if ($model->companyOrderStorages()->sum('quantity')) exception('该库位有商品，不能删除');
    }

}